#include "searchservice.h"
#include "Core/dbcontroler.h"
#include "Core/deallogicobj.h"
#include "Core/mytcpserver.h"
#include "Core/tcpuserconnect.h"
SearchService::SearchService(QObject *parent) : QObject(parent)
{

}

void SearchService::_doFindOnline(std::shared_ptr<PDU>Msg, TcpUserConnect *TcpUser)
{
    char * InfoChar=new char[1000000]{'\0'};
    memcpy(InfoChar,(char*)Msg.get()+sizeof(PDU),Msg->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    delete [] InfoChar;
    InfoChar=nullptr;
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject InfoJson=dc.object();
    QString selfUn=InfoJson["USER"].toString();


    QList<QPair<QString,QString>>result;
    DBControler::get_instance()
            ->SearchOnline(selfUn.toUtf8().toStdString().c_str(),result);
    QJsonObject Body;
    QJsonArray ALLMSG;
    for(auto & UMSG:result){
        QJsonObject oneUMSG;
        oneUMSG["USER"]=UMSG.first;
        oneUMSG["ETC"]=UMSG.second;
        ALLMSG.push_back(oneUMSG);
    }
    Body["ONLINEARRAY"]=ALLMSG;

    TcpUser->SendMsg(MSG_TYPE_SEARCH_ONLINE_RESPOND,Body);

    return;
}

void SearchService::_doAllNetScanAll(std::shared_ptr<PDU>Msg, TcpUserConnect *TcpUser)
{
    char * InfoChar=new char[1000000]{'\0'};
    memcpy(InfoChar,(char*)Msg.get()+sizeof(PDU),Msg->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    delete [] InfoChar;
    InfoChar=nullptr;
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject InfoJson=dc.object();
    QString selfUn=InfoJson["SCANUSER"].toString();


    QList<QPair<QString,QString>>result=DBControler::get_instance()
            ->_AllNetScanUser(selfUn.toUtf8().toStdString().c_str());

    QJsonObject Body;
    QJsonArray ALLMSG;
    for(auto & UMSG:result){
        QJsonObject oneUMSG;
        oneUMSG["USER"]=UMSG.first;
        oneUMSG["ETC"]=UMSG.second;
        ALLMSG.push_back(oneUMSG);
    }
    Body["ALLNETARRAY"]=ALLMSG;
    qDebug()<<Body;
    TcpUser->SendMsg(MSG_TYPE_ALLNET_SCAN_ALL_RESPOND,Body);
    return;
}
